import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-18
 * Time: 21:26
 */
public class search_Range {
    public int[] searchRange(int[] nums, int target) {
        int[] result = new int[2];
        Arrays.fill(result, -1);
        if (nums == null || nums.length == 0) {
            return result;
        }
        int start = 0, end = nums.length - 1;
        //寻找左边界(大于等于target)
        while (start < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] >= target) {
                end = mid;
            } else if (nums[mid] < target) {
                start = mid + 1;
            }
        }

        if (nums[start] != target) return result;
        else result[0] = end;

        //寻找右边界(小于等于target)
        start = 0;
        end = nums.length - 1;
        while (start < end) {
            int mid = start + (end - start + 1) / 2;
            if (nums[mid] <= target) {
                start = mid;
            } else if (nums[mid] > target) {
                end = mid - 1;
            }
        }
        result[1] = start;
        return result;
    }
}
